//
//  UIViewControllerExtension.swift
//  JJAlbum
//
//  Created by 吴梓杭 on 2025/10/14.
//

import Foundation
import UIKit

extension UIViewController {
    var visibleViewControllerIfExist: UIViewController? {
        if let presentedViewController = self.presentedViewController {
            return presentedViewController.visibleViewControllerIfExist
        }
        
        if let navigationController = self as? UINavigationController {
            return navigationController.visibleViewController?.visibleViewControllerIfExist
        }
        
        if let tabBarController = self as? UITabBarController {
            return tabBarController.selectedViewController?.visibleViewControllerIfExist
        }
        
        return self
    }
}

struct QMUIHelper {
    static var visibleViewController: UIViewController? {
        guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else {
            return nil
        }
        return rootViewController.visibleViewControllerIfExist
    }
}
